<?php
/**
 * @author mofeng
 */

namespace App\Filters;

use App\User;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Http\Request;

class ThreadsFilters extends Filters
{
    protected $filters = ['by', 'popularity','unanswered'];

    public function by($username)
    {
        $user = User::where('name', $username)->firstOrfail();

        return $this->builder->where('user_id', $user->id);
    }

    /**
     * @return mixed
     */
    public function popularity()
    {
        $this->builder->getQuery()->orders = []; //清空其他排序情况

        return $this->builder->orderBy('replies_count','desc');
    }

    public function unanswered()
    {
        return $this->builder->where('replies_count',0);
    }
}
